home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / Arch / Soundblaster / audio.c next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  3.1 KB  |  141 lines

  1. /* soundblaster/audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4. /* IMPORTANT NOTE: I can't check that this file works.
  5.  */
  6.  
  7. /* $Id: audio.c,v 4.2 1995/02/01 16:43:47 espie Exp $
  8.  * $Log: audio.c,v $
  9.  * Revision 4.2  1995/02/01  16:43:47  espie
  10.  * 23 bit samples.
  11.  *
  12.  * Revision 4.2  1995/02/01  16:43:47  espie
  13.  * 23 bit samples.
  14.  *
  15.  * Revision 4.0  1994/01/11  18:01:44  espie
  16.  * *** empty log message ***
  17.  *
  18.  * Revision 1.2  1993/12/26  18:54:21  Espie
  19.  * Handle errors better.
  20.  *
  21.  * Revision 1.1  1993/12/26  00:55:53  Espie
  22.  * Initial revision
  23.  *
  24.  * Revision 3.4  1993/11/17  15:31:16  espie
  25.  * *** empty log message ***
  26.  *
  27.  * Revision 3.3  1992/12/03  15:00:50  espie
  28.  * restore stty.
  29.  *
  30.  * Revision 3.1  1992/11/19  20:44:47  espie
  31.  * Protracker commands.
  32.  *
  33.  * Revision 3.0  1992/11/18  16:08:05  espie
  34.  * New release.
  35.  *
  36.  * Revision 1.5  1992/11/17  15:38:00  espie
  37.  * Dummy discard_buffer()
  38.  * Added stereo option (kind of).
  39.  */
  40.  
  41. #include "defs.h"
  42. #include "extern.h"
  43. #include <i386at/sblast.h>
  44.  
  45. ID("$Id: audio.c,v 4.2 1995/02/01 16:43:47 espie Exp $")
  46.  
  47. LOCAL unsigned char *buffer;/* buffer for ready-to-play samples */
  48. LOCAL int buf_index;   /* can't call this index, conflicts with index(3) */
  49. FILE *audio;            /* /dev/sb_dsp */
  50.  
  51. /* are we playing stereo or not ? */
  52. LOCAL int stereo;
  53. /* 256th of primary/secondary source for that side. */
  54. LOCAL int primary, secondary;
  55.  
  56. void set_mix(percent)
  57. int percent;
  58.     {
  59.     percent *= 256;
  60.     percent /= 100;
  61.     primary = percent;
  62.     secondary = 512 - percent;
  63.     }
  64.  
  65. int open_audio(f, s)
  66. int f;
  67. int s;
  68.     {
  69.     audio = fopen("/dev/sb_dsp", "w");
  70.     if (!audio)
  71.         end_all("Error opening audio device");
  72.  
  73.     stereo = s;
  74.     if (ioctl(fileno(audio), DSP_IOCTL_STEREO, stereo) == -1)
  75.         end_all("Error setting stereo/mono");
  76.  
  77.     if (stereo)
  78.         f *= 2;     /* XXX Stereo takes twice the speed */
  79.  
  80.     if (f == 0)
  81.         f = -1;     /* read current frequency from driver */
  82.  
  83.     if (ioctl(fileno(audio), DSP_IOCTL_SPEED, &f) == -1)
  84.         end_all("Error setting frequency");
  85.  
  86.     buffer = malloc(sizeof(SAMPLE) * f);    /* Stereo makes x2 */
  87.     buf_index = 0;
  88.  
  89.     if (stereo)         /* tacky, I know.. */
  90.         return f/ 2;
  91.     else
  92.         return f;
  93.     }
  94.  
  95. void output_samples(left, right)
  96. int left, right;
  97.     {
  98.     if (stereo)
  99.         {
  100.         buffer[buf_index++] = (((left * primary + right * secondary) / 65536)
  101.              + (1 << 15)) >> 8;
  102.         buffer[buf_index++] = (((right * primary + left * secondary) / 65536)
  103.              + (1 << 15)) >> 8;
  104.         }
  105.     else
  106.         buffer[buf_index++] = (left + right + (1 << 23)) >> 16;
  107.     }
  108.  
  109. void discard_buffer()
  110.     {
  111.     /* not implemented */
  112.     }
  113.  
  114. void flush_buffer()
  115.     {
  116.     if (fwrite(buffer, sizeof(*buffer), buf_index, audio) != buf_index)
  117.         notice("fwrite didn't write all the bytes ?");
  118.     buf_index = 0;
  119.     }
  120.  
  121. /*
  122.  * Closing the BSD SBlast sound device waits for all pending samples to play.
  123.  * I think SysV aborts, so you might have to flush manually with ioctl()
  124.  */
  125. void close_audio()
  126.     {
  127.     fclose(audio);
  128.     free(buffer);
  129.     }
  130.  
  131. int update_frequency()
  132.     {
  133.     /* not implemented */
  134.     return 0;
  135.     }
  136.  
  137. void set_synchro()
  138.     {
  139.     /* not implemented */
  140.     }
  141.